package com.origin.niuke.math;

/**
 * NC106 三个数的最大乘积
 * 算法：数学
 * 找到数组中最大的三个数和最小的两个数
 * 最大值在（最大的三个数相乘和最大的数乘上最小的两个数）之中
 *
 * @author yezh
 * @data 2022/11/12 22:53
 */
public class NC106 {

    public long solve(int[] A) {
        int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE;
        int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
        for (int num : A) {
            if (num >= max1) {
                max3 = max2;
                max2 = max1;
                max1 = num;
            } else if (num >= max2) {
                max3 = max2;
                max2 = num;
            } else if (num > max3) max3 = num;
            if (num <= min1) {
                min2 = min1;
                min1 = num;
            } else if (num < min2) min2 = num;
        }
        return Math.max((long) max1 * max2 * max3, (long) max1 * min1 * min2);
    }

}
